import argparse
import re
def python_lines(f_path):
    num_d = 0
    num_k = 0
    num_z = 0

    z_cond = re.compile(r'^\s*#')
    with open(f_path, 'r', encoding='utf-8') as file:
        for line in file:
            line = line.strip()
            if not line:
                num_k += 1
            elif z_cond.match(line):
                num_z += 1
            else:
                num_d += 1
    return num_d, num_k, num_z

def main():
    arpa = argparse.ArgumentParser(description="代码各行类型统计")
    arpa.add_argument("-p", dest="python_lines", action="store_true", help="统计python文件的各行类型")
    arpa.add_argument("file", help="文件")
    res = arpa.parse_args()

    if not res.python_lines:
        print("请输入指令：-p python文件的各行类型")
        return

    try:
        if res.python_lines:
            d, k, z = python_lines(res.file)
            print(f"代码行数: {d}")
            print(f"空行数: {k}")
            print(f"注释行数: {z}")


    except FileNotFoundError:
        print(f"代码文件 '{res.file}' 不存在")

if __name__ == "__main__":
    main()